home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0006_FAST Mode 13h Line Draw.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-23  |  4KB  |  154 lines

  1. {
  2. ===========================================================================
  3.  BBS: Beta Connection
  4. Date: 08-20-93 (09:59)             Number: 2208
  5. From: SEAN PALMER                  Refer#: NONE
  6.   To: ALL                           Recvd: NO  
  7. Subj: FAST mode 13h Li (Part 1)      Conf: (232) T_Pascal_R
  8. ---------------------------------------------------------------------------
  9. Hey! Here's THE fastest mode 13h bresenham's line drawing function ever.
  10. (I think...prove me wrong, please!!)
  11.  
  12. It's written for TP 6 or better, uses BASM. If you don't know assembly, just
  13. put it in a unit and don't worry about how it works. If you do, fine.
  14. Some good optimizations in there...
  15.  
  16. Have fun! If anyone wants the mostly-pascal equivalent, let me know.
  17. It's still fast.
  18.  
  19. {by Sean Palmer}
  20. {public domain}
  21.  
  22. var color:byte;
  23.  
  24. procedure line(x,y,x2,y2:word);assembler;asm {mode 13}
  25.  mov ax,$A000
  26.  mov es,ax
  27.  mov bx,x
  28.  mov ax,y
  29.  mov cx,x2
  30.  mov si,y2
  31.  cmp ax,si
  32.  jbe @NO_SWAP   {always draw downwards}
  33.  xchg bx,cx
  34.  xchg ax,si
  35. @NO_SWAP:
  36.  sub si,ax         {yd (pos)}
  37.  sub cx,bx         {xd (+/-)}
  38.  cld               {set up direction flag}
  39.  jns @H_ABS
  40.  neg cx      {make x positive}
  41.  std
  42. @H_ABS:
  43.  mov di,320
  44.  mul di
  45.  mov di,ax
  46.  add di,bx   {di:adr}
  47.  or si,si
  48.  jnz @NOT_H
  49. {horizontal line}
  50.  cld
  51.  mov al,color
  52.  inc cx
  53.  rep stosb
  54.  jmp @EXIT
  55. @NOT_H:
  56.  or cx,cx
  57.  jnz @NOT_V
  58. {vertical line}
  59.  cld
  60.  mov al,color
  61.  mov cx,si
  62.  inc cx
  63.  mov bx,320-1
  64. @VLINE_LOOP:
  65.  stosb
  66.  add di,bx
  67.  loop @VLINE_LOOP
  68.  jmp @EXIT
  69. @NOT_V:
  70.  cmp cx,si    {which is greater distance?}
  71.  lahf         {then store flags}
  72.  ja @H_IND
  73.  xchg cx,si   {swap for redundant calcs}
  74. @H_IND:
  75.  mov dx,si    {inc2 (adjustment when decision var rolls over)}
  76.  sub dx,cx
  77.  shl dx,1
  78.  shl si,1     {inc1 (step for decision var)}
  79.  mov bx,si    {decision var, tells when we need to go secondary direction}
  80.  sub bx,cx
  81.  inc cx
  82.  push bp      {need another register to hold often-used constant}
  83.  mov bp,320
  84.  mov al,color
  85.  sahf         {restore flags}
  86.  jb @DIAG_V
  87. {mostly-horizontal diagonal line}
  88.  or bx,bx     {set flags initially, set at end of loop for other iterations}
  89. @LH:
  90.  stosb        {plot and move x, doesn't affect flags}
  91.  jns @SH      {decision var rollover in bx?}
  92.  add bx,si
  93.  loop @LH   {doesn't affect flags}
  94.  jmp @X
  95. @SH:
  96.  add di,bp
  97.  add bx,dx
  98.  loop @LH   {doesn't affect flags}
  99.  jmp @X
  100. @DIAG_V:
  101. {mostly-vertical diagonal line}
  102.  or bx,bx    {set flags initially, set at end of loop for other iterations}
  103. @LV:
  104.  mov es:[di],al   {plot, doesn't affect flags}
  105.  jns @SV          {decision var rollover in bx?}
  106.  add di,bp        {update y coord}
  107.  add bx,si
  108.  loop @LV         {doesn't affect flags}
  109.  jmp @X
  110. @SV:
  111.  scasb   {sure this is superfluous but it's a quick way to inc/dec x coord!}
  112.  add di,bp        {update y coord}
  113.  add bx,dx
  114.  loop @LV         {doesn't affect flags}
  115. @X:
  116.  pop bp
  117. @EXIT:
  118.  end;
  119.  
  120. var k,i,j:word;
  121. begin
  122.  asm mov ax,$13; int $10; end;
  123.  for k:=0 to 31 do begin
  124.   i:=k*10;
  125.   j:=k*6;
  126.   color:=14;
  127.   line(159,99,i,0);
  128.   color:=13;
  129.   line(160,99,319,j);
  130.   color:=12;
  131.   line(160,100,319-i,199);
  132.   color:=11;
  133.   line(159,100,0,199-j);
  134.   i:=k*9;
  135.   j:=k*5;
  136.   color:=6;
  137.   line(i,0,159,99);
  138.   color:=5;
  139.   line(319,j,160,99);
  140.   color:=4;
  141.   line(319-i,199,160,100);
  142.   color:=3;
  143.   line(0,199-j,159,100);
  144.   end;
  145.  Readln;
  146.  asm mov ax,3; int $10; end;
  147.  end.
  148.  
  149. ... I'm not unemployed, I'm indefinitely leisured.
  150. ___ Blue Wave/QWK v2.12
  151. ---
  152.  * deltaComm Online 919-481-9399 - 10 lines
  153.  * PostLink(tm) v1.06  DELTA (#22) : RelayNet(tm) HUB
  154.